package com.camera.yl.di

import android.app.ActivityManager
import android.content.Context
import android.os.Process
import android.util.Log
import com.camera.base.inerface.CameraServiceSingleton
import com.camera.base.inerface.ICPInterface
import com.camera.base.inerface.WebrtcServiceSingleton
import com.camera.base.inerface.YLWebrtcInterface
import com.camera.base.v.models.SystemNavBarViewModel
import com.camera.routemodule.routeinterface.GBAppInterface
import com.camera.routemodule.routeinterface.RtspInterface
import com.camera.routemodule.routeinterface.YLWebSocketInterface
import com.camera.yl.ICPAPP
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
class AppModule {
    val TAG = AppModule::class.java.simpleName
    var mICPAPP:ICPAPP? = null;
    var mContext: Context? = null;
    var mYLWebSocketInterface: YLWebSocketInterface? = null;
    var mICPInterface:ICPInterface? = null;
    var mYLWebrtcInterface:YLWebrtcInterface? = null;
    var mGBAppInterface: GBAppInterface? = null;
    var mRtspInterface: RtspInterface? = null;
    @Provides
    @Singleton //单例
    fun providesApplication(@ApplicationContext context: Context): ICPAPP?  {
        Log.e(TAG, "providesApplication: " )
        if (context.getPackageName() == getCurrentProcessName(context)) {
            mICPAPP = context as ICPAPP;

        }
        return mICPAPP
    }
    @Provides
    fun providesApplicationContext(@ApplicationContext context: Context): Context? {
        if (context.getPackageName() == getCurrentProcessName(context)) {
            mContext = context;
        }
        return mContext
    }
    @Provides
    @Singleton //单例
    fun providesICPCameraServer(application:ICPAPP?): ICPInterface? {
        if (application != null && application?.getPackageName() == getCurrentProcessName(application)) {
            mICPInterface = CameraServiceSingleton.getInstance()
            mICPInterface?.init(application)
        }
        return mICPInterface
    }

    //20240530 webrtc
    @Provides
    @Singleton //单例
    fun providesWebrtcServer(application:ICPAPP?): YLWebrtcInterface? {
        if (application != null && application?.getPackageName() == getCurrentProcessName(application)) {
            mYLWebrtcInterface = WebrtcServiceSingleton.getInstance();

        }
        return mYLWebrtcInterface
    }
    private fun getCurrentProcessName(context: Context): String? {
        val pid = Process.myPid()
        val mActivityManager = context
            .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (appProcess in mActivityManager
            .runningAppProcesses) {
            if (appProcess.pid == pid) {
                return appProcess.processName
            }
        }
        return null
    }
    @Provides
    @Singleton //单例
    fun providesSystemNavBarViewModel(application:ICPAPP?): SystemNavBarViewModel {
        return application!!.mSystemNavBarViewModel
    }
}
